//
//  YCBaseView.swift
//  swift微博
//
//  Created by 姚伟聪 on 15/12/15.
//  Copyright © 2015年 姚伟聪. All rights reserved.
//

import UIKit

//协议
protocol YCBaseViewDelegate: NSObjectProtocol{

    func baseViewLoginClick()
    func baseViewRegisteClick()

}


class YCBaseView: UIView {

    //代理属性
    weak var delegate: YCBaseViewDelegate?
  
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        addUI()
        
        loginBtn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        registerBtn.addTarget(self, action: "registeClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /**
     改变访客视图
     
     - parameter imageName:  图片名称
     - parameter textString: 文字内容
     */
    func changeVisiterView(imageName: String,textString: String){
        
        circle.hidden = true
        house.image = UIImage(named: imageName)
        textLab.text = textString
        
    }
    ///开始动画
    func startAnimation(){
    
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        //动画完成不移除
        animation.removedOnCompletion = false
        circle.layer.addAnimation(animation, forKey: nil)
        
    }
    
    /// 暂停旋转
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = circle.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        circle.layer.speed = 0
        
        // 设置动画偏移时间
        circle.layer.timeOffset = pauseTime
    }
    
    /// 恢复动画
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = circle.layer.timeOffset
        
        // 设置动画速度为1
        circle.layer.speed = 1
        
        // 重置偏移时间
        circle.layer.timeOffset = 0
        
        // 重置开始时间
        circle.layer.beginTime = 0
        
        // 计算开始时间
        let timeSincePause = circle.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        // 设置开始时间
        circle.layer.beginTime = timeSincePause
    }

    //MARK: - 按钮点击
     func loginClick(){
        
        //有值就执行,没值就不执行
        delegate?.baseViewLoginClick()
        
    }
    
     func registeClick(){
    
        delegate?.baseViewRegisteClick()
    }
    //MARK: - 添加UI
    private func addUI(){

        addSubview(circle)
        addSubview(hazyView)
        addSubview(house)
        addSubview(textLab)
        addSubview(loginBtn)
        addSubview(registerBtn)
        //取消Autoresizing,默认是开启的
        circle.translatesAutoresizingMaskIntoConstraints = false
        hazyView.translatesAutoresizingMaskIntoConstraints = false
        house.translatesAutoresizingMaskIntoConstraints = false
        textLab.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        
        //圆圈图片
        circle.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        addConstraint(NSLayoutConstraint(item: circle, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circle, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        //房子图片
        house.image = UIImage(named: "visitordiscover_feed_image_house")
        addConstraint(NSLayoutConstraint(item: house, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: circle, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: house, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: circle, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        //文字
        textLab.text = "关注一些人，回这里看看又什么消息"
        addConstraint(NSLayoutConstraint(item: textLab, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: circle, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        addConstraint(NSLayoutConstraint(item: textLab, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: circle, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: textLab, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 40))
         addConstraint(NSLayoutConstraint(item: textLab, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: -40))
        //注册按钮
        registerBtn.setTitle("注册", forState: UIControlState.Normal)
        registerBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        addConstraints([
            NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLab, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10),
            NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: textLab, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0),
            NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100),
            NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 50)])
        //登录按钮
        loginBtn.setTitle("登录", forState: UIControlState.Normal)
        loginBtn.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        addConstraints([
            NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLab, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10),
            NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: textLab, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0),
            NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100),
            NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 50)])
        //蒙版图片
        addConstraint(NSLayoutConstraint(item: hazyView , attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: hazyView , attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: loginBtn, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: hazyView , attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: hazyView , attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
    }
    
    //MARK: - 懒加载
    //房子图片
    lazy var house:UIImageView = {
        let house = UIImageView()
        return house
    }()
    //圆圈背景图片
    lazy var circle: UIImageView = {
        let circle = UIImageView()
        return circle
    }()
    //注册按钮
    lazy var registerBtn: UIButton = {
        let btn = UIButton()
        return btn
    }()
    //登录按钮
    lazy var loginBtn: UIButton = {
        let btn = UIButton()
        return btn
    }()
    //文字
    lazy var textLab: UILabel = {
        let lab = UILabel()
        lab.numberOfLines = 0
        lab.textAlignment = NSTextAlignment.Center
        return lab
    }()
    //蒙版
    lazy var hazyView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    

}
